//app.js
//1:加载模块
const express = require("express");
const pool  = require("./pool");
//2:创建express对象
var app = express();

//服务器node.js 允许跨域访问配置项
//2.1:引入跨域模块   
const cors = require("cors");
//2.2:配置允许哪个程序跨域访问 脚手架   11:16
app.use(cors({
  origin:[
    "http://127.0.0.1:8080","http://localhost:8080"],
  credentials:true
}))

//3:指定静态目录
//服务器指定目录 绝对路径 出错
app.use(express.static(__dirname+"/public"));

//4:绑定监听端口
app.listen(3000);

//登录
const qs = require("querystring");
app.get("/login",(req,res)=>{
  // console.log(req.query)
    
  var json1 = [
    {"username":"mike","password":123},
    {"username":"herry","password":456},
    {"username":"jack","password":789} 
  ];
  var data = JSON.stringify(json1)
  var obj = JSON.parse(data);//转换为json对象
  var $uname = req.query.username;
  var $upwd = req.query.password;
  for(var i=0;i<obj.length;i++){
    if($uname == obj[i].username && $upwd == obj[i].password){
        res.send({msg:"登录成功",code:11})
        return
    }
  }
  res.send({msg:"登录失败",code:0})
  return
})

 //注册
 app.post("/register",(req,res)=>{
  req.on('data',(buf)=>{
    console.log(11)
    res.send("注册成功")
  })

})

